package com.qupai.config;


import com.qupai.interceptor.JwtTokenAdminInterceptor;
import com.qupai.interceptor.JwtTokenAuctionInterceptor;
import com.qupai.interceptor.JwtTokenUserInterceptor;
import com.qupai.properties.AuthProperties;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Resource
    private JwtTokenAuctionInterceptor jwtTokenAuctionInterceptor;
    @Resource
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
    @Resource
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;
    @Resource
    private AuthProperties authProperties;

    /**
     * 注册自定义拦截器
     *
     * @param registry 注册
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenAuctionInterceptor)
                .addPathPatterns(authProperties.getIncludeAuctionPaths())
                .excludePathPatterns(authProperties.getExcludeAuctionPaths());
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns(authProperties.getIncludeAdminPaths())
                .excludePathPatterns(authProperties.getExcludeAdminPaths());
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns(authProperties.getIncludeUserPaths())
                .excludePathPatterns(authProperties.getExcludeUserPaths());
    }

    /**
     * 设置静态资源映射
     * @param registry 注册
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始设置静态资源映射...");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
